<template>
  <div class="page-hot-search">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="title"
            :label="$t('shop.hotTitle') + ':'"
          >
            <el-input
              v-model="searchForm.title"
              type="text"
              clearable
              :placeholder="$t('shop.hotTitle')"
            />
          </el-form-item>
          <el-form-item
            prop="content"
            :label="$t('shop.hotContent') + ':'"
          >
            <el-input
              v-model="searchForm.content"
              type="text"
              clearable
              :placeholder="$t('shop.hotContent')"
            />
          </el-form-item>
          <el-form-item
            prop="status"
            :label="$t('shop.enableStatus') + ':'"
          >
            <el-select
              v-model="searchForm.status"
              clearable
              :placeholder="$t('shop.enableStatus')"
            >
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch(searchFormRef)"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <el-checkbox
          v-model="selectAll"
          class="all-check-btn"
          @change="onSelectAll"
        >
          {{
            $t('brand.selectAll')
          }}
        </el-checkbox>
        <span
          v-if="dataListSelections.length"
          class="had-selected"
        >{{ $t('brand.selected') }} {{ dataListSelections.length }}</span>
        <div
          v-if="isAuth('admin:hotSearch:save')"
          class="default-btn primary-btn"
          @click="onAddOrUpdate()"
        >
          {{ $t('crud.addTitle') }}
        </div>
        <div
          v-if="isAuth('admin:hotSearch:delete')"
          :class="[!dataListSelections.length ? 'disabled-btn' : '', 'default-btn']"
          @click.stop="onDelete"
        >
          {{ $t('sys.batchDelete') }}
        </div>
      </div>
      <div class="table-con">
        <el-table
          ref="hotTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @selection-change="onSelectionChange"
        >
          <el-table-column
            type="selection"
            width="65"
          />
          <el-table-column
            :label="$t('shop.hotTitle')"
            prop="title"
            align="left"
            min-width="330"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.title }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="content"
            min-width="360"
            :label="$t('shop.hotContent')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.content }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            min-width="260"
            prop="recDate"
            :label="$t('shop.recDate')"
          />
          <el-table-column
            align="left"
            min-width="190"
            prop="seq"
            :label="$t('temp.sequence')"
          />
          <el-table-column
            align="left"
            min-width="190"
            :label="$t('shop.enableStatus')"
          >
            <template #default="scope">
              <span>{{ [$t('publics.offline'), $t('publics.normal')][scope.row.status] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            align="center"
            width="190"
            :label="$t('crud.menu')"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('admin:hotSearch:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row)"
                >
                  {{ $t('brand.revise') }}
                </div>
                <div
                  v-if="isAuth('admin:hotSearch:delete')"
                  class="default-btn text-btn"
                  @click="onDelete(scope.row, scope.index)"
                >
                  {{ $t('brand.delete') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './add-or-update.vue'
import { isAuth } from '@/utils/index'
import { ElMessageBox, ElMessage } from 'element-plus'

const status = [
  {
    label: $t('publics.offline'),
    value: 0
  },
  {
    label: $t('publics.normal'),
    value: 1
  }
]
const page = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
onMounted(() => {
  onGetDataList(page.value)
})

// 获取数据列表
let theParams = null // 保存上次点击查询的请求条件
let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([]) // 数据列表
const searchForm = reactive({
  title: '',
  content: '',
  status: null
})
const sortParam = 0 // 0无 1录入时间 2顺序
const sortType = 0 // 0无 1 正序 2倒序
const dataListLoading = ref(false)
const onGetDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    theParams = JSON.parse(JSON.stringify(searchForm))
    tempSearchForm = {
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 20,
      sortParam,
      sortType
    }
  } else {
    tempSearchForm.current = page.value ? page.value.currentPage : 1
    tempSearchForm.size = page.value ? page.value.pageSize : 20
  }
  http({
    url: http.adornUrl('/admin/hotSearch/page'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, theParams))
  }).then(({ data }) => {
    page.value.total = data.total
    page.value.pageSize = data.size
    page.value.currentPage = data.current
    dataList.value = data.records
    dataListLoading.value = false
  })
}
/**
 * 全选按钮
 */
const hotTableRef = ref(0) // 拿子组件的ref
const selectAll = ref(false)
const onSelectAll = () => {
  selectAll.value = hotTableRef.value.selection?.length < dataList.value?.length
  hotTableRef.value?.toggleAllSelection()
}
// 多选回调
const dataListSelections = ref([])
const onSelectionChange = list => {
  dataListSelections.value = list
  selectAll.value = list.length === dataList.value.length
}
// 新增 / 修改
const addOrUpdateRef = ref(null)
const addOrUpdateVisible = ref(false)
const onAddOrUpdate = id => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}
// 点击查询
const onSearch = (newData = false) => {
  page.value.currentPage = 1
  onGetDataList(page.value, newData)
}
// 删除
const onDelete = row => {
  const ids = row.hotSearchId ? [row.hotSearchId] : dataListSelections.value.map(item => {
    return item.hotSearchId
  })
  if (!ids.length) {
    return
  }
  ElMessageBox.confirm(
    `${$t('sys.makeSure')}${row.hotSearchId ? $t('text.delBtn') : $t('sys.batchDelete')}${$t(
      'text.menu'
    )}?`,
    $t('text.tips'),
    {
      confirmButtonText: $t('crud.filter.submitBtn'),
      cancelButtonText: $t('crud.filter.cancelBtn'),
      type: 'warning'
    }
  ).then(() => {
    http({
      url: http.adornUrl('/admin/hotSearch'),
      method: 'delete',
      data: http.adornData(ids, false)
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  })
}
// 刷新回调用
const refreshChange = () => {
  onGetDataList(page.value)
}
const onPageSizeChange = val => {
  page.value.pageSize = val
  onGetDataList(page.value)
}
const onPageChange = val => {
  page.value.currentPage = val
  onGetDataList(page.value)
}
// 重置该表单
const searchFormRef = ref(null)
const onResetSearch = formName => {
  formName.resetFields()
}
</script>
